lib.use(['element', 'table', 'form','tablePlug'], function () {
    var table = layui.table;
    var tablePlug = layui.tablePlug;
    var gaugeSheetTable;
    var probId=common.util.getParam("probId");

    //初始表格基础参数
    table.set(common.layui.table.config(tablePlug));


    // 渲染表格
    gaugeSheetTable = table.render({
            elem: '#gaugeSheetTable'
            , url: common.util.createUrl('/sale/gaugeSheet/list')
            , height: '320px'
            ,toolbar: '#toolbar'
            ,where:{probId:probId}
            , cols: [[
                {type:'checkbox'}
               ,{type: 'numbers', title: '编号' }
                ,{ field: 'entId',  hide: true}
                ,{ field: 'id',  hide: true}
                ,{ field: 'probId',  hide: true}
                ,{ title: '预订单编码', field: 'probNo',width:140}
                ,{ title: '量尺日期', field: 'gaugeDate' ,width:120}
                ,{ title: '预约日期', field: 'estDate',width:120 }
                ,{ title: '安装方式', field: 'instalModel' ,width:120}
                ,{ title: '量尺面积', field: 'area' ,width:120}
                ,{ title: '水平情况', field: 'level' ,width:120}
                ,{ title: '订单状态', field: 'gaugeState',width:120 ,templet: function (row) { return common.dist.getValue("sale.GaugeSheet_GaugeStateEnum",row.gaugeState);}}
                ,{ field: 'remark',  hide: true}
                ,{ field: 'createUserId',  hide: true}
                ,{ field: 'createUserName',  hide: true}
                ,{ title: '创建时间',width:160, field: 'createTime'}
                ,{ field: 'updateUserId',  hide: true}
                ,{ field: 'updateUserName',  hide: true }
                //,{ title: '修改时间', field: 'updateTime' }
            ]]
    });


    /**
     * 查询表格
     * @param params
     */
    function searchTable(params) {
        params=common.from.serializeObject('#searchFrm');
        table.reload('gaugeSheetTable',{where:params,page: {curr:1}})
    }

    //监听行单击事件（单击事件为：rowDouble）
    table.on('rowDouble(gaugeSheetTableFilter)', function(obj){
        update(obj)
    });

    //头工具栏事件
    table.on('toolbar(gaugeSheetTableFilter)', function(obj){
        if(obj.event=='add') {
            var config = {
                busStyle:1,
                title:'添加量尺单',
                content: ['sale/gaugeSheet/gaugeSheetForm.html?probId='+probId], //iframe的url，no代表不显示滚动条
            }
            parent.common.util.openWindow(busCommon.windowConfig(config))

        }else if(obj.event=="export"){
            var params=common.from.serializeObject('#searchFrm');
            params['pages']=gaugeSheetTable.config.page.pages;
            params['limit']=gaugeSheetTable.config.page.limit;
            params['probId']=probId;
            parent.parent.common.util.exportWindows('/sale/gaugeSheet/export',params);
        }else if(obj.event=='del'){
            del(table.checkStatus('gaugeSheetTable'))
        }else if(obj.event=='check'){
            check(table.checkStatus('gaugeSheetTable'))
        }else if(obj.event=='refresh'){
            searchTable();
        }
    })
    /**
     * 修改
     * @param obj
     */
    function update(obj) {
        if (!common.auth.hasPermission("sale:gaugeSheet:update")) {
            return;
        }
        var postData = [];
        if (obj.data.constructor === Array) {
            postData = obj.data.map(function (item) {
                return item['id'];
            });
        } else {
            postData = [obj.data.id]
        }
        if (postData.length != 1) return;
        var url= 'sale/gaugeSheet/gaugeSheetForm.html?id=' + postData[0] + "&probId=" + probId;
        var config = {
            content: url, //iframe的url，no代表不显示滚动条
            busStyle:1,
            title:'修改量尺单'
        }
        parent.common.util.openWindow(busCommon.windowConfig(config))
    }


    /**
     * 审核
     * @param obj
     */
    function check(obj) {
        if(!common.auth.hasPermission("sale:gaugeSheet:check")){return;}
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item;
            });
        }else{
            postData=[obj.data]
        }

        if(postData.length!=1){parent.parent.common.util.error('请选择一条记录进行操作');return;}
        if(obj.data[0].gaugeState!=1){parent.parent.common.util.error('[{0}] 当前订单已审核,禁止审核'.format(obj.data[0].probNo));return;}
        parent.parent.common.util.confirm('确认审核!', function(index){
            console.log('Config Check param is', postData);
            parent.parent.common.ajax.get('/sale/gaugeSheet/check/'+postData[0].id, null, function (resp) {
                searchTable();
                parent.parent.layer.close(index);
                parent.parent.common.util.success("审核成功")
            },function(resp){
                top.layer.close(index);
                top.common.util.error(resp.msg)
            })

        });
    }
    /**
     * 删除
     * @param obj
     */
    function del(obj) {
        if(!common.auth.hasPermission("sale:gaugeSheet:delete")){return;}
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item['id'];
            });
        }else{
            postData=[obj.data.id]
        }

        if(postData.length!=1){top.common.util.error('请选择一条记录');return;}
        if(obj.data[0].gaugeState==2){top.common.util.error('当前量尺单已审核,,禁止删除');return;}
        top.layer.confirm('确认删除!', function(index){
            console.log('Config Del param is', postData);
            common.ajax.post('/sale/gaugeSheet/delete', postData, function (resp) {
                searchTable();
                parent.parent.layer.close(index);
            },function(resp){
                top.layer.close(index);
                top.common.util.error(resp.msg)
            })

        });
    }
    window.formCallBack=function () {
        searchTable();
    }
});
